package com.zhuiYun.project.api.newDaily.service.impl;

import com.zhuiYun.project.api.commoditydetail.entity.Commodity;
import com.zhuiYun.project.api.newDaily.entity.UserIntegral;
import com.zhuiYun.project.api.newDaily.entity.dto.SlideshowDTO;
import com.zhuiYun.project.api.newDaily.entity.dto.UserCouponDTO;
import com.zhuiYun.project.api.newDaily.mapper.NewDailyMapper;
import com.zhuiYun.project.api.newDaily.model.UserCouponModel;
import com.zhuiYun.project.api.newDaily.service.NewDailyService;
import com.zhuiYun.project.common.errorCode.EmErrorCode;
import com.zhuiYun.project.common.response.CommonResult;
import com.zhuiYun.project.util.ObjectUtils;
import com.zhuiYun.project.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @Description TODO 每日上新（小程序）
 * @ClassName NewDailyServiceImpl
 * @Author HH
 * @Date 2023/4/5 14:20
 **/
@Service
public class NewDailyServiceImpl implements NewDailyService {

    @Autowired
    NewDailyMapper newDailyMapper;

    /**
     * @Description 查询轮播图
     * @Author HH
     * @Date 2023/4/5 14:48
     * @Return com.zhuiYun.project.common.response.CommonResult

     */
    @Override
    public CommonResult selectSlideShow() {
        List<SlideshowDTO> slideshows = newDailyMapper.selectSlideShow();
        if(slideshows != null && slideshows.size() > 0){
            return CommonResult.ok(slideshows);
        }else {
            return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
        }
    }

    /**
     * @Description 查询今日上新
     * @Author HH
     * @Date 2023/4/5 14:19
     * @Return com.zhuiYun.project.common.response.CommonResult

     */
    @Override
    public CommonResult selectCommodityNew() {
        List<Commodity> commodities = newDailyMapper.selectCommodityNew();
        if(commodities != null && commodities.size() > 0){
            return CommonResult.ok(commodities);
        }else {
            return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
        }
    }

    /**
     * @Description 查询拼团好货
     * @Author HH
     * @Date 2023/4/6 10:47
     * @Return com.zhuiYun.project.common.response.CommonResult

     */
    @Override
    public CommonResult selectCommodity() {
        List<Commodity> commodities = newDailyMapper.selectCommodity();
        if(commodities != null && commodities.size() > 0){
            return CommonResult.ok(commodities);
        }else {
            return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
        }
    }

    /**
     * @Description 根据用户id查询用户可用的优惠券
     * @Author HH
     * @Date 2023/4/6 21:24
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param userId
     */
    @Override
    public CommonResult selectUserCoupon(Integer userId) {
        List<UserCouponDTO> coupons = newDailyMapper.selectUserCoupon(userId);
        if(coupons != null && coupons.size() > 0){
            return CommonResult.ok(coupons);
        }else {
            return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
        }
    }

    /**
     * @Description 添加用户优惠券
     * @Author HH
     * @Date 2023/4/7 9:53
     * @Return int
     * @param userCouponModel
     */
    @Override
    public CommonResult insertUserCoupon(UserCouponModel userCouponModel) {
        if(newDailyMapper.insertUserCoupon(userCouponModel) > 0){
            return CommonResult.ok();
        }else {
            return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
        }
    }

    /**
     * @Description 查询用户积分
     * @Author HH
     * @Date 2023/4/13 16:21
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param userId
     */
    @Override
    public CommonResult selectUserIntegral(Integer userId) {
        UserIntegral userIntegral = newDailyMapper.selectUserIntegral(userId);
        if(ObjectUtils.isNotEmpty(userIntegral)){
            return CommonResult.ok(userIntegral);
        }else {
            return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
        }
    }

    /**
     * @Description 查询用户今天是否签到
     * @Author HH
     * @Date 2023/4/13 19:42
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param userId
     */
    @Override
    public CommonResult selectSignInState(Integer userId) {
        // 获取日期
        LocalDateTime now = LocalDateTime.now();
        // 拼接key
        String keySuffix = now.format(DateTimeFormatter.ofPattern(":yyyy-MM"));
        String key = "user_sign_in_" + userId + keySuffix;
        // 获取今天是本月的第几天
        int dayOfMonth = now.getDayOfMonth();
        if(RedisUtil.getBit(key, dayOfMonth - 1)){
            return CommonResult.ok();
        }else {
            return CommonResult.error();
        }
    }

    /**
     * @Description 用户签到
     * @Author HH
     * @Date 2023/4/13 16:24
     * @Return com.zhuiYun.project.common.response.CommonResult
     * @param userIntegral
     */
    @Override
    public CommonResult signIn(UserIntegral userIntegral) {
        // 获取日期
        LocalDateTime now = LocalDateTime.now();
        // 拼接key
        String keySuffix = now.format(DateTimeFormatter.ofPattern(":yyyy-MM"));
        String key = "user_sign_in_" + userIntegral.getUserId() + keySuffix;
        // 获取今天是本月的第几天
        int dayOfMonth = now.getDayOfMonth();
        // 写入redis setbit key offset 1
        if(RedisUtil.getBit(key, dayOfMonth - 1)){
            return CommonResult.error();
        }else {
//            dayOfMonth -1
            RedisUtil.setBit(key, dayOfMonth - 1, true);
            if(ObjectUtils.isNotEmpty(newDailyMapper.selectUserIntegral(userIntegral.getUserId()))){
                userIntegral.setIntegral(userIntegral.getIntegral() + 10);
                if(newDailyMapper.updateUserIntegral(userIntegral) > 0){
                    return CommonResult.ok();
                }else {
                    return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
                }
            }else {
                userIntegral.setIntegral(10);
                if(newDailyMapper.insertUserIntegral(userIntegral) > 0){
                    return CommonResult.ok();
                }else {
                    return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
                }
            }
        }
    }
}
